package com.example.yzj16478026;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.yzj16478026.utils.PropertiesUtils;

public class LoginActivity extends Activity {
    private EditText username,password;
    private Button btn_login;
//    private String usernameval,passwordval;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Intent intent =getIntent();
        //初始化组件绑定
        init();
        //为组件绑定登录事件
        LoginEventBind();
        String msg=intent.getStringExtra("msg");
        if (username!=null){
            Toast toast= Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_LONG);
            toast.show();
        }
    }

    private void LoginEventBind() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usn=username.getText().toString();
                String pwd=password.getText().toString();
                if (PropertiesUtils.getPwdForRegName(usn)!=null){
                    if (pwd.equals(PropertiesUtils.getPwdForRegName(usn))){
                        Intent intent=new Intent(LoginActivity.this,OrderActivity.class);
                        intent.putExtra("usa","严子江");
                        startActivity(intent);
                    }else {
                        Toast toast=Toast.makeText(LoginActivity.this,"密码错误，登录失败!",Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }else {
                    Toast toast=Toast.makeText(LoginActivity.this,"用户名不存在，登录失败!",Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
    }

    private void init() {
        username=findViewById(R.id.username);
        password=findViewById(R.id.password);
        btn_login=findViewById(R.id.btn_login);
    }

}
